package com.example.routelink.server1;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;

import javax.annotation.Resource;
import java.net.InetAddress;

@SpringBootApplication
public class RoutelinkServer1Application implements CommandLineRunner {

    @Autowired
    private ServletWebServerApplicationContext servletWebServerApplicationContext;

    @Value("${spring.application.name}")
    private String appName;

    @Value("${nacos.address}")
    private String nacosAddress;


    public static void main(String[] args) {
        SpringApplication.run(RoutelinkServer1Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        InetAddress localHost = InetAddress.getLocalHost();
        String ipAddress = localHost.getHostAddress();
        NamingService namingService = NacosFactory.createNamingService(nacosAddress);
        namingService.registerInstance(appName, ipAddress, servletWebServerApplicationContext.getWebServer().getPort());
    }
}
